package com.itheima.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.jwt.JWT;
import com.alibaba.fastjson2.JSON;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import com.itheima.vo.EmployeeVo;
import com.itheima.vo.PagingQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author :WuJunJie
 * packageName :com.itheima.service.impl
 * fileName :EmployeeServiceImpl
 * creationTime :2022-11-20 17:24
 */
@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public EmployeeVo findByUsernameAndPassword(String username, String password) {
        Employee employee = employeeMapper.selectByUsernameAndPassword(username, password);
        if (employee == null) {
            return null;
        }
        EmployeeVo employeeVo = getEmployeeVo(employee);
        final String token = JWT.create()
                .setNotBefore(DateUtil.date())
                .setKey("admin".getBytes())
                .sign();
        employeeVo.setToken(token);
        redisTemplate.opsForValue().set("employee_token:"+token, JSON.toJSONString(employee),30, TimeUnit.MINUTES);
        return employeeVo;
    }

    @Override
    public Boolean insertEmployee(Employee employee) {
        Employee employeeInDB = employeeMapper.selectByUsername(employee.getUsername());
        if (employeeInDB!=null){
            return false;
        }
        String id = IdUtil.getSnowflakeNextIdStr();
        employee.setId(id);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser(ThreadLocalUtil.getid());
        employee.setUpdateUser(ThreadLocalUtil.getid());
        Integer integer = employeeMapper.insertEmployee(employee);
        return integer>0;
    }

    @Override
    public PagingQueryVo<EmployeeVo> selectPage(String name, Integer page, Integer pageSize) {
        Integer start = (page - 1) * pageSize;
        List<Employee> employees = employeeMapper.selectPage(name, start, pageSize);
        PagingQueryVo<EmployeeVo> pagingQueryVo = new PagingQueryVo<>();
        List<EmployeeVo> employeeVos=new ArrayList<>();
        employees.forEach(s->{
            employeeVos.add(getEmployeeVo(s));
        });

        pagingQueryVo.setRecords(employeeVos);
        pagingQueryVo.setCurrent(page);
        pagingQueryVo.setSize(pageSize);
        pagingQueryVo.setTotal(employeeMapper.countTotal(name));
        return pagingQueryVo;
    }

    @Override
    public EmployeeVo selectById(String id) {
        Employee employee = employeeMapper.selectById(id);
        return getEmployeeVo(employee);
    }

    @Override
    public Boolean updateEmployee(Employee employee) {
        Employee employeeInDB = employeeMapper.selectById(employee.getId());
        if (employeeInDB==null){
            return false;
        }
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(ThreadLocalUtil.getid());
        Integer update=employeeMapper.updateEmployee(employee);
            return update>0;
    }

    public EmployeeVo getEmployeeVo(Employee employee){
        EmployeeVo employeeVo = new EmployeeVo();
        BeanUtils.copyProperties(employee,employeeVo);
        return employeeVo;
    }
}
